
const { src, dest, parallel, series, watch } = require('gulp')

// 自动加载插件
const loadPlugins = require("gulp-load-plugins")

const plugins = loadPlugins()
// 样式编译插件
const sass = require("gulp-sass")(require('sass'))
// 脚本编译插件
// const babel = require("gulp-babel")
// 页面编译插件
// const swig = require("gulp-swig")
// 图片文件转换和压缩
// const imagemin = require('gulp-imagemin')
// 清除指定文件
const del = require('del')
// 提供开发服务器的模块
const bowserSync = require("browser-sync")

const bs = bowserSync.create()

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

const clean = () => {
  return del(['dist'])
}

// 样式编译方法
const style = () => {
  return src("src/assets/styles/**.scss", { base: 'src' })
       .pipe(sass({ outputStyle: "expanded" }))
       .pipe(dest('dist'))
}

// 脚本编译方法
const script = () => {
  return src("src/assets/scripts/**.js", { base: "src" })
      .pipe(plugins.babel({ presets: ["@babel/preset-env"] }))
      .pipe(dest('dist'))
}

const page = () => {
  return src("src/**.html", { base: "src" })
      .pipe(plugins.swig({ data }))
      .pipe(dest('dist'))
}

// 图片转换压缩方法
const image = () => {
  return src("src/assets/images/**", { base: "src" })
      .pipe(plugins.imagemin())
      .pipe(dest('dist'))
}
const font = () => {
  return src("src/assets/font/**", { base: "src" })
      .pipe(plugins.imagemin())
      .pipe(dest('dist'))
}

const extra = () => {
  return src("public/**", { base: "public" })
        .pipe(dest('dist'))
}

const useref = () => {
  return src("dist/*.html", { base: "dist" })
       .pipe(plugins.useref({ searchPath: ["dist", "."] }))
      //   html js css
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
          collapseWhitespace: true,
          minifyCss: true,
          minifyJS: true
        })))
       .pipe(dest('dist'))
}

const serve = () => {
  watch("src/assets/styles/**.scss", style),
  watch("src/assets/scripts/**.js", script)
  watch("src/**.html", page)
  // watch("src/assets/images/**", image)
  // watch("src/assets/font/**", font)
  // watch("public/**", extra)
  watch([
    "src/assets/images/**",
    "src/assets/font/**",
    "public/**"
  ], bs.reload)
  bs.init({
    // 提示
    notify: false,
    // 端口
    port: 8080,
    // 当指定文件下的文件改变时会更新浏览器
    files: "dist/**",
    server: {
      baseDir: ["dist", "src", "public"],
      // 启动时会先查看routes的配置在执行baseDir 
      routes: {
        "/node_modules": "node_modules"
      }
    }
  })
}

// 创建组合任务
const compile = parallel(style, script, page, image, font)
// 先清除在编译， 否则会出现编译好的任务被清除
const build = series(clean, parallel(compile, extra))

const develop = series(compile, serve)

module.exports = {
  compile,
  build,
  serve,
  develop,
  clean,
  useref
}